<template>
	<view>
		<u-tabbar :value="selectedTabbar" activeColor="#00baad" :fixed="true" :placeholder="true" :safeAreaInsetBottom="true" @change="handleTabbarChanged">
			<block v-for="(item, index) in tabbarList" :key="index">
				<u-tabbar-item v-if="index !== 2" :text="item.text" :icon="item.icon" :name="index"></u-tabbar-item>
				<audioPlayerVue style="margin-top: 5rpx" v-else />
			</block>
		</u-tabbar>
	</view>
</template>

<script>
import audioPlayerVue from './audio-player.vue';
export default {
	name: 'tabbar',
	components: {
		audioPlayerVue
	},
	props: {
		selectedTabbar: {
			type: Number,
			default: 0
		}
	},
	data() {
		return {
			tabbarList: [
				{
					text: '首页',
					icon: 'home',
					path: '/pages/index/index'
				},
				{
					text: 'VIP推荐',
					icon: 'level',
					path: '/pages/user/vip'
				},
				{
					text: '',
					icon: '',
					path: ''
				},
				{
					text: '工具',
					icon: 'bookmark',
					path: '/pages/tool/tool'
				},
				{
					text: '我的',
					icon: 'account',
					path: '/pages/user/user'
				}
			]
		};
	},
	created() {
		this.setTabbar();
	},
	methods: {
		setTabbar() {
			let List1 = [
				{
					text: '首页',
					icon: 'home',
					path: '/pages/index/index'
				},
				{
					text: 'VIP推荐',
					icon: 'level',
					path: '/pages/user/vip'
				},
				{
					text: '',
					icon: '',
					path: ''
				},
				{
					text: '工具',
					icon: 'bookmark',
					path: '/pages/tool/tool'
				},
				{
					text: '我的',
					icon: 'account',
					path: '/pages/user/user'
				}
			];
			let List2 = [
				{
					text: '首页',
					icon: 'home',
					path: '/pages/index/index'
				},
				{
					text: '足迹',
					icon: 'tags',
					path: '/pages/list/history'
				},
				{
					text: '',
					icon: '',
					path: ''
				},
				{
					text: '工具',
					icon: 'bookmark',
					path: '/pages/tool/tool'
				},
				{
					text: '我的',
					icon: 'account',
					path: '/pages/user/user'
				}
			];
			uni.request({
				url: 'https://yc.aiaud.cn/api/index/getBooksPayStatus',
				header: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				method: 'POST',
				success: (response) => {
					let resdata = response.data;
					if (resdata && resdata.data) {
						console.log('服务器返回数据:', resdata.data);
						if (resdata.data.ispay == 0) {
							this.tabbarList = List2;
						} else if (resdata.data.ispay == 1) {
							this.tabbarList = List1;
						}
						// uni.$u.mpShare.path = 'pages/index/index?share_id=' + resdata.invite_id;
					} else {
						console.error('无效的服务器响应:', response);
					}
				},
				fail: (err) => {
					console.error('Request failed', err);
				}
			});
		},
		// 处理tab切换事件
		handleTabbarChanged(index) {
			console.log('handleTabbarChanged', index);

			// 获取选中tab对应的路径
			let tabbar = this.tabbarList[index];
			if (tabbar) {
				// 跳转到对应的页面
				uni.$u.route({
					type: 'tab',
					url: tabbar.path,
					params: {}
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped></style>
